package com.briup.smartcity_group1.web.controller.auth;

import com.briup.smartcity_group1.bean.basic.BaseConfig;
import com.briup.smartcity_group1.consts.BaseConfigDbConsts;
import com.briup.smartcity_group1.consts.BeanNameConsts;
import com.briup.smartcity_group1.consts.DataMessageConsts;
import com.briup.smartcity_group1.service.IBaseConfigService;
import com.briup.smartcity_group1.utils.result.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author WangChen
 * @date 2023/1/5 18:03
 */
@RestController
@RequestMapping(BeanNameConsts.BASE_CONFIG)
public class BaseConfigController {

    @Autowired
    private IBaseConfigService service;

    @DeleteMapping
    public void deleteById(@RequestParam(required = true) Integer id){
        service.deleteById(id);
        Result.setDataMessage(DataMessageConsts.BASE_CONFIG_DELETE_SUCCESS);
    }

    @GetMapping(value = "/yes")
    public BaseConfig getByYes(){
        return service.currentConfigYes();
    }

    @GetMapping(value = "/page")
    public PageInfo<BaseConfig> getAllByPage(@RequestParam(required = true,defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = true,defaultValue = "3") Integer pageSize){
        return service.findAllWithPage(pageNum, pageSize);
    }

    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void save(@RequestParam(required = true) String configIcon,
                     @RequestParam(required = true) String configInfo,
                     @RequestParam(required = true) String configName){

        BaseConfig baseConfig = new BaseConfig();
        baseConfig.setConfigIcon(configIcon);
        baseConfig.setConfigInfo(configInfo);
        baseConfig.setConfigName(configName);
        baseConfig.setConfigStatus(BaseConfigDbConsts.DISABLED);

        service.saveConfig(baseConfig);

        Result.setDataMessage(DataMessageConsts.BASE_CONFIG_SAVE_SUCCESS);
    }

    @PutMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void update(@RequestParam(required = true) String configIcon,
                       @RequestParam(required = true) Integer configId,
                       @RequestParam(required = true) String configInfo,
                       @RequestParam(required = true) String configName){

        BaseConfig baseConfig = new BaseConfig();
        baseConfig.setConfigIcon(configIcon);
        baseConfig.setConfigId(configId);
        baseConfig.setConfigInfo(configInfo);
        baseConfig.setConfigName(configName);

        service.updateConfig(baseConfig);
        Result.setDataMessage(DataMessageConsts.BASE_CONFIG_UPDATE_SUCCESS);
    }

    @PutMapping(value = "status",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void updateStatus(@RequestParam(required = true) Integer id,
                             @RequestParam(required = true) Integer status){

        service.changeConfigStatus(id,status);
        Result.setDataMessage(DataMessageConsts.BASE_CONFIG_UPDATE_SUCCESS);
    }

}
